﻿using System;
using System.Globalization;
using System.Text.RegularExpressions;

public class Localization : IFormatProvider
{
    static Localization _inst;

    public static readonly Regex CN_AREA_REGEX = new Regex(@"^(cn|zh)$", RegexOptions.IgnoreCase);

    public static NumberStyles NumStyle
    {
        get
        {
            if (_inst == null)
                _inst = new Localization();
            return _inst.mNumStyles;
        }
    }

    public static Localization Universal
    {
        get
        {
            if (_inst == null)
            {
                _inst = new Localization();
            }
            return _inst;
        }
    }

    public static CultureInfo GlobalCulture
    {
        get
        {
            if (_inst == null)
            {
                _inst = new Localization();
            }
            return _inst.mGlobalCulture;
        }
    }

    public static bool TryParse(string str, out float value)
    {
        if (_inst == null)
            _inst = new Localization();
        return float.TryParse(str, _inst.mNumStyles, _inst, out value);
    }

    public static float ParseFloat(string str)
    {

        if (_inst == null)
            _inst = new Localization();
        return float.Parse(str, _inst);
    }

    public static bool TryParse(string str, out double value)
    {
        if (_inst == null)
            _inst = new Localization();
        return double.TryParse(str, _inst.mNumStyles, _inst, out value);
    }

    public static double ParseDouble(string str)
    {
        if (_inst == null)
            _inst = new Localization();
        return double.Parse(str, _inst);
    }

    public static bool TryParse(string str, out int value)
    {
        if (_inst == null)
            _inst = new Localization();
        return int.TryParse(str, NumberStyles.Integer, _inst, out value);
    }

    public static bool TryParse(string str, out int value, NumberStyles styles)
    {
        if (_inst == null)
            _inst = new Localization();
        return int.TryParse(str, styles, _inst, out value);
    }

    public static int ParseInt(string str)
    {
        if (_inst == null)
            _inst = new Localization();
        return int.Parse(str, _inst);
    }

    public static bool TryParse(string str, out long value)
    {
        if (_inst == null)
            _inst = new Localization();
        return long.TryParse(str, _inst.mNumStyles, _inst, out value);
    }

    public static long ParseLong(string str)
    {
        if (_inst == null)
            _inst = new Localization();
        return long.Parse(str, _inst);
    }

    public static bool TryParse(string str, out uint value)
    {
        if (_inst == null)
            _inst = new Localization();
        return uint.TryParse(str, NumberStyles.Integer, _inst, out value);
    }

    public static bool TryParse(string str, out ulong value)
    {
        if (_inst == null)
            _inst = new Localization();
        return ulong.TryParse(str, NumberStyles.Integer, _inst, out value);
    }

    public static string ToString(float value)
    {
        if (_inst == null)
            _inst = new Localization();
        return value.ToString(_inst.mGlobalCulture);
    }

    public static string ToString(float value, string format)
    {
        if (_inst == null)
            _inst = new Localization();
        return value.ToString(format, _inst.mGlobalCulture);
    }

    public static string ToString(int value)
    {
        if (_inst == null)
            _inst = new Localization();
        return value.ToString(_inst.mGlobalCulture);
    }

    public static string ToString(int value, string format)
    {
        if (_inst == null)
            _inst = new Localization();
        return value.ToString(format, _inst.mGlobalCulture);
    }

    public static string ToString(byte value)
    {
        if (_inst == null)
            _inst = new Localization();
        return value.ToString(_inst.mGlobalCulture);
    }

    public static string ToString(byte value, string format)
    {
        if (_inst == null)
            _inst = new Localization();
        return value.ToString(format, _inst.mGlobalCulture);
    }

    public static string ToString(long value)
    {
        if (_inst == null)
            _inst = new Localization();
        return value.ToString(_inst.mGlobalCulture);
    }

    public static string ToString(long value, string format)
    {
        if (_inst == null)
            _inst = new Localization();
        return value.ToString(format, _inst.mGlobalCulture);
    }

    public static string ToString(double value)
    {
        if (_inst == null)
            _inst = new Localization();
        return value.ToString(_inst.mGlobalCulture);
    }

    public static string ToString(double value, string format)
    {
        if (_inst == null)
            _inst = new Localization();
        return value.ToString(format, _inst.mGlobalCulture);
    }

    public static string Format(string format, params object[] args)
    {
        if (_inst == null)
            _inst = new Localization();
        return string.Format(_inst, format, args);
    }

    public static string ToDiscount(float discount, string area)
    {
        if (_inst == null)
            _inst = new Localization();
        if (CN_AREA_REGEX.IsMatch(area))
            return string.Format(_inst.mGlobalCulture, "{0:0.#}折", discount * 10f);
        else
            return string.Format(_inst.mGlobalCulture, "{0:0%} OFF", 1 - discount);
    }

    #region IFormatProvider implement

    CultureInfo mGlobalCulture;
    NumberStyles mNumStyles;

    private Localization()
    {
        mGlobalCulture = new CultureInfo("en");
        mNumStyles = NumberStyles.AllowLeadingWhite | NumberStyles.AllowTrailingWhite
            | NumberStyles.AllowLeadingSign | NumberStyles.AllowDecimalPoint
            | NumberStyles.AllowThousands | NumberStyles.AllowExponent;
        //mLocalCulture = Thread.CurrentThread.CurrentCulture;
        float f = 0;
        f.ToString();
    }

    public object GetFormat(Type formatType)
    {
        return mGlobalCulture.GetFormat(formatType);
    }

    #endregion
}